package com.note.web;

import com.note.common.core.controller.BaseController;
import com.note.common.core.domain.AjaxResult;
import com.note.common.core.redis.RedisCache;
import com.note.constant.PvConstant;
import com.note.dto.PvDTO;
import com.note.system.service.ISysUserService;
import com.note.util.CalendarUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 网站统计
 */
@Api("网站统计controller")
@Controller
@RequestMapping("/webStatistics")
public class WebStatisticsController extends BaseController {


    @Autowired
    RedisCache redisCache;
    @Autowired
    ISysUserService sysUserService;

    @ResponseBody
    @ApiOperation("获取 前几天的 日活曲线")
    @ApiImplicitParams({
            @ApiImplicitParam(name="rangeDate",value="前几天(包括当天),比如前5天，前10天",dataType="int", paramType = "query")
    })
    @GetMapping("/getPVList")
    public AjaxResult getPVList(Integer  rangeDate){
        PvDTO pvDTO = new PvDTO();
        List<PvDTO.Statistics> noLoginList = new ArrayList<>();
        List<PvDTO.Statistics> loginList = new ArrayList<>();
        pvDTO.setLoginList(loginList);
        pvDTO.setNoLoginList(noLoginList);
        for(int i = rangeDate - 1; i >= 0; i--){
            String date = CalendarUtil.getBeforeDateStr(i,"yyyy-MM-dd");
            //获取date 这天的未登录pv
            Long noLoingPv = redisCache.bitCount(PvConstant.NOLOGIN_PV+":"+date);
            //获取date 这天的登录pv
            Long loingPv = redisCache.bitCount(PvConstant.LOGIN_PV+":"+date);

            PvDTO.Statistics noLoingStatistics = new PvDTO.Statistics();
            noLoingStatistics.setDatePv(date);
            noLoingStatistics.setNum(noLoingPv);
            noLoginList.add(noLoingStatistics);

            PvDTO.Statistics loingStatistics = new PvDTO.Statistics();
            loingStatistics.setDatePv(date);
            loingStatistics.setNum(loingPv);
            loginList.add(loingStatistics);
        }


        int userNum = sysUserService.getUserNum();
        pvDTO.setAllUserNum(userNum);
        return AjaxResult.success(pvDTO);
    }





}
